﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace ExIF35
{
    public partial class Form2 : Form
    {
        String filename = "";
        Roll thisRoll= new Roll();
        int count = 0;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 newExp = new Form1(txtPhotographer.Text, count);
            newExp.ShowDialog();
            listView1.Items.Add(new ListViewItem(new string[]{newExp.exposureNumber,newExp.exposureDateTime, "F/"+newExp.exposureAperture, newExp.exposureFocalLength+" mm", newExp.exposureSpeed, newExp.exposureTitle}));
            thisRoll.add(newExp.theExposure);
            count = Convert.ToInt32(newExp.exposureNumber); 
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            NameValueCollection nvc = new NameValueCollection();
            nvc = new configMan().getConfig();
            if (nvc.Get("validConfig").Equals("true"))
            {
                txtPhotographer.Text = nvc.Get("Photographer");
                ISO.Text = nvc.Get("ISO");
                film.Text = nvc.Get("Film");
                Roll.Text = nvc.Get("Roll");
                cameramodel.Text = nvc.Get("CameraModel");
                cameramake.Text = nvc.Get("CameraMake");
                filename = nvc.Get("filename");
            }

            if (!filename.Equals(""))
            {
                loadFile();
            }

            if (Roll.Text.Equals(""))
            {
                Roll.Text = "1";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {


            if (filename.Equals(""))
            {
                saveFileDialog1.ShowDialog();
                filename = saveFileDialog1.FileName;
            }
            

            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("Photographer", txtPhotographer.Text);
            nvc.Add("CameraMake", cameramake.Text);
            nvc.Add("CameraModel", cameramodel.Text);
            nvc.Add("Film", film.Text);
            nvc.Add("Roll", Roll.Text);
            nvc.Add("ISO", ISO.Text);
            nvc.Add("filename", filename);
            new configMan().setConfig(nvc);

            saveFile();

        }
        public void saveFile()
        {
            thisRoll.FilmType = film.Text;
            thisRoll.ISO = ISO.Text;
            thisRoll.number = Roll.Text;
            thisRoll.cameramake = cameramake.Text;
            thisRoll.cameramodel = cameramodel.Text;

            ObjectXMLSerializer<Roll>.Save(thisRoll, filename);

            MessageBox.Show("Roll saved to " + filename);
        }
        public void loadFile()
        {
            thisRoll = ObjectXMLSerializer<Roll>.Load(filename);
            Roll.Text = thisRoll.number;
            film.Text = thisRoll.FilmType;
            ISO.Text = thisRoll.ISO;
            cameramake.Text = thisRoll.cameramake;
            cameramodel.Text = thisRoll.cameramodel;

            foreach (Exposure exp in thisRoll.exposures)
            {
                listView1.Items.Add(new ListViewItem(new string[] { exp.Number, exp.Date, "F/"+exp.Aperture, exp.FocalLength+" mm", exp.Speed, exp.Title }));
            }
            count = Convert.ToInt32(thisRoll.exposures[thisRoll.exposures.Count-1].Number);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 1)
            {
                if (filename.Equals(""))
                {
                    saveFileDialog1.ShowDialog();
                    filename = saveFileDialog1.FileName;
                }
                saveFile();
            }
            openFileDialog1.ShowDialog();


        }

        private void button4_Click(object sender, EventArgs e)
        {
            filename = "";
            thisRoll = new Roll();
            listView1.Items.Clear();
            int RollNum = Convert.ToInt32(Roll.Text) + 1;
            Roll.Text = RollNum.ToString();

        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            filename = openFileDialog1.FileName;
            loadFile();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form3 mapEXIF = new Form3(thisRoll);
            mapEXIF.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("File Name: " + thisRoll.exposures[0].ImageFile);
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            Form4 WinForm4 = new Form4();
            WinForm4.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            editExposure();
            
        }
        private void editExposure()
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                int temp = listView1.SelectedIndices[0];
                Form1 newExp = new Form1(thisRoll.exposures[temp], temp);
                newExp.ShowDialog();
                if (newExp.exposureNumber != null)
                {
                    listView1.Items[temp] = new ListViewItem(new string[] { newExp.exposureNumber, newExp.exposureDateTime, "F/" + newExp.exposureAperture, newExp.exposureFocalLength + " mm", newExp.exposureSpeed, newExp.exposureTitle });
                    thisRoll.exposures[temp] = newExp.theExposure;
                }
            }
            else
            {
                MessageBox.Show("You must select an item to edit");
            }
        }
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            editExposure();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deleteExposure();
        }
        private void deleteExposure()
        {
            DialogResult result = MessageBox.Show("Are you sure you wish to delete exposure " + thisRoll.exposures[listView1.SelectedIndices[0]].Number + "?", "Delete Exposure", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                thisRoll.exposures.RemoveAt(listView1.SelectedIndices[0]);
                listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
                
                
            }
        }
    }
}
